package com.ruoyi.service.achievements.impl;

import com.ruoyi.common.config.Global;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.mapper.achievements.AcvJobMapper;
import com.ruoyi.service.achievements.AcvJobService;
import com.ruoyi.util.ExcelUtils;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Service
public class AcvJobServiceImpl implements AcvJobService {

    @Autowired
    AcvJobMapper acvJobMapper;

    @Override
    public List<Map> selectCurrencyJobList(Map map) {
        return acvJobMapper.selectCurrencyJobList(map);
    }

    @Override
    public int addDeptJob(Map map) {
        return acvJobMapper.addDeptJob(map);
    }

    @Override
    public int insertDeptTemplateByJobId(Map map) {
        return acvJobMapper.insertDeptTemplateByJobId(map);
    }

    @Override
    public List<Map> selectCurrencyTemplateList(String jobId) {
        return acvJobMapper.selectCurrencyTemplateList(jobId);
    }

    @Override
    @Transactional
    public int updateTemplate(MultipartFile file, String jobId) {
        int num;
        //删除这个通用岗位模板
        acvJobMapper.deleteTemplateByJobId(jobId);
        //将使用这个通用岗位的部门岗位ID的模板设置为失效
        acvJobMapper.setDeptTemplateIsFalse(jobId);
        //插入新的通用模板数据
        List<Map<String, Object>> excelData = ExcelUtils.getExcelData(file, new String[]{"assessmentIndex", "describe", "goal", "standard", "distribution"});
        num = acvJobMapper.insertTemplateByNewFile(excelData, jobId);
        if (num > 0) {
            //依次插入使用这个通用岗位模板的部门岗位模板
            List<String> deptJobIds = acvJobMapper.selectDeptJobIdByJobId(jobId);
            for (String deptJobId : deptJobIds) {
                Map map = new HashMap();
                map.put("deptJobId", deptJobId);
                map.put("jobId", jobId);
                acvJobMapper.insertDeptTemplateByJobId(map);
            }
        }
        return num;
    }

    @Override
    public AjaxResult downloadTemplateByJobId(String jobId) {
        List<Map> list = acvJobMapper.selectCurrencyTemplateList(jobId);
        try {
            HSSFWorkbook wb = new HSSFWorkbook();
            HSSFSheet sheet = wb.createSheet("通用模板");
            //新增一行
            HSSFRow row = sheet.createRow(0);
            HSSFCellStyle txtStyle1 = wb.createCellStyle();
            HSSFFont txtFont = wb.createFont();
            txtFont.setFontName("宋体");
            txtFont.setFontHeightInPoints((short) 9);
            txtFont.setBold(true);
            txtStyle1.setFont(txtFont);
            txtStyle1.setWrapText(true);
            txtStyle1.setAlignment(HorizontalAlignment.CENTER);
            txtStyle1.setVerticalAlignment(VerticalAlignment.CENTER);
            txtStyle1.setBorderBottom(BorderStyle.THIN);
            txtStyle1.setBorderLeft(BorderStyle.THIN);
            txtStyle1.setBorderTop(BorderStyle.THIN);
            txtStyle1.setBorderRight(BorderStyle.THIN);
            row.createCell(0).setCellValue("考核指标");
            row.createCell(1).setCellValue("指标描述/计算公式");
            row.createCell(2).setCellValue("达成目标");
            row.createCell(3).setCellValue("评分标准");
            row.createCell(4).setCellValue("指标配分(满分100分)");
            row.setHeightInPoints(50);
            for (Cell cell : row) {
                cell.setCellStyle(txtStyle1);
            }

            HSSFCellStyle txtStyle = wb.createCellStyle();
            HSSFFont txtFont1 = wb.createFont();
            txtFont1.setFontName("宋体");
            txtFont1.setFontHeightInPoints((short) 9);
            txtStyle.setFont(txtFont1);
            txtStyle.setWrapText(true);
            txtStyle.setAlignment(HorizontalAlignment.CENTER);
            txtStyle.setVerticalAlignment(VerticalAlignment.CENTER);
            txtStyle.setBorderBottom(BorderStyle.THIN);
            txtStyle.setBorderLeft(BorderStyle.THIN);
            txtStyle.setBorderTop(BorderStyle.THIN);
            txtStyle.setBorderRight(BorderStyle.THIN);

            for (int i = 0; i < list.size(); i++) {
                row = sheet.createRow(i + 1);
                Map map = list.get(i);
                row.createCell(0).setCellValue(map.get("assessment_index").toString());
                row.createCell(1).setCellValue(map.get("describe").toString());
                row.createCell(2).setCellValue(map.get("goal").toString());
                row.createCell(3).setCellValue(map.get("standard").toString());
                row.createCell(4).setCellValue(map.get("distribution").toString());
                row.setHeightInPoints(40);
                for (Cell cell : row) {
                    cell.setCellStyle(txtStyle);
                }
            }

            sheet.setColumnWidth(0, 35 * 256);
            sheet.setColumnWidth(1, 42 * 256);
            sheet.setColumnWidth(2, 32 * 256);
            sheet.setColumnWidth(3, 40 * 256);
            sheet.setColumnWidth(4, 20 * 256);

            String filePath = Global.getDownloadPath() + "通用模板.xls";
            FileOutputStream fileOut = new FileOutputStream(filePath);
            wb.write(fileOut);
            fileOut.close();
            return AjaxResult.success(0);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("失败");
        }
    }

    @Override
    public int addJob(MultipartFile file, String jobName) {
        List<Map<String, Object>> excelData = ExcelUtils.getExcelData(file, new String[]{"assessmentIndex", "describe", "goal", "standard", "distribution"});
        String jobId = UUID.randomUUID().toString().replaceAll("-", "").toUpperCase();
        //添加岗位表
        acvJobMapper.addJob(jobId, jobName);
        //添加模板数据表
        return acvJobMapper.insertTemplateByNewFile(excelData, jobId);
    }

}
